import os

from my_json import load_json, save_json, download_json, bulid_download_path
from spider_url import school_rank_url

basic_path = str(os.path.dirname(__file__)) + "/"
school_id_path = basic_path + "../basic_info/school_id.json"
download_path = bulid_download_path(basic_path)


def init():
    url = school_rank_url()
    school_id = load_json(school_id_path)
    d = {}
    for k, v in school_id.items():
        try:
            school0 = download_json(url.to_string(k), download_path + "school_" + k + ".json")
        except ValueError:
            print("school rank error: " + k)
            continue
        if k in d:
            raise ValueError("school rank duplicate key")
        d[k] = school0["data"]
    save_json(basic_path + "school_rank.json", d)


init()
